package work3.work1;

import Uti.ScannerUti;
import work3.work1.Customer;

import java.util.Arrays;

public class CustomerGoodsAdmin {
    Customer[] customers = new Customer[5];
    int num;

    public CustomerGoodsAdmin() {
        show();
    }


    void show() {
        while (true) {
            System.out.println();
            System.out.println("请输入：1-购物信息录入;2-信息查询;3-根据购物价格排序;0-退出客户选购信息管理系统");
            int kk = ScannerUti.getInt();
            if (kk == 1) {//实现多个客户购买商品信息的录入
                inputCustomerInfo();
            } else if (kk == 2) {
                queryInformation();
            } else if (kk == 3) {
                compareCustomerInfo();
            } else if (kk == 0) {
                break;
            }


        }
    }

    private void compareCustomerInfo() {
        System.out.println("请输入：1-升序;2-降序");
        int kk = ScannerUti.getInt();
        if (kk == 1) {
            //升序
            myCmp(true);
        }

        else if (kk == 2) {
            //降序
            myCmp(false);
        }
        print();

    }
    void myCmp(boolean bool){

            for(int i=0;i<num;i++){
                for(int j=0;j<num-i-1;j++){
                    if((bool&&customers[j].getTotal()>customers[j+1].getTotal())
                            ||(!bool&&customers[j].getTotal()<customers[j+1].getTotal())){
                        Customer t = new Customer();
                        t=customers[j];
                        customers[j]=customers[j+1];
                        customers[j+1]=t;
                    }
                }
            }


    }
    private void queryInformation() {
        System.out.println("请输入客户姓名：");
        String s = ScannerUti.getSting();
        if (s.equals(" ")) {
            System.out.println("客户统计表");
            System.out.println("--------------------");
            print();
        } else {
            for (int i = 0; i < num; i++) {
                if (customers[i].getName().equals(s)) {
                    customers[i].print();
                }
            }
        }
    }

    ;

    void inputCustomerInfo() {

        while (true) {
            Customer customer = new Customer();
            System.out.println("请输入客户名称，年龄，以','号分隔");
            String s = ScannerUti.getSting();
            String[] split = s.split(",");
            if (split.length != 2) {
                System.out.println("输入有误请重新输入");
                continue;
            }
            customer.setName(split[0]);
            ;
            try {
                customer.setAge(Integer.parseInt(split[1]));
            } catch (NumberFormatException e) {
                System.out.println("输入有误请重新输入");
            }

            customer.inputGoodsInfo();

            customer.print();
            System.out.println("客户" + customer.getName() + "录入完成");
            customers[num++] = customer;
            if (num >= 5) {
                customers = Arrays.copyOf(customers, num + 5);
            }
            break;

        }

    }

    ;

    void print() {
        for (int i = 0; i < num; i++) {
                customers[i].print();
        }
    }

    ;


}
